package com.budwk.app.sqfwV2.service.roomSafety;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.exception.ServiceException;
import com.budwk.app.sqfw.constants.SqfwFormSummaryConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwV2.dto.RoomSafetyAssistantDTO;
import com.budwk.app.sys.services.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;

/**
 * 房安助手
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-06-15  10:17
 * @Description: TODO
 */
@IocBean
@Slf4j
public class RoomSafetyAssistantService {

    @Inject
    private SysUserService sysUserService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    /**
     * 新增隐患
     * @param json
     */
    @Aop(TransAop.READ_COMMITTED)
    public void add(JSONObject json, boolean isThird) {
        log.info("新增隐患：" + json);
        String surracId = null;
        if (isThird) {
            surracId = json.getString("surracId"); // 隐患id
            if (StringUtils.isBlank(surracId)) throw new ServiceException("隐患id不能为空");

            Cnd cnd = Cnd.NEW();
            cnd.and("param_data->>'id'", "=", surracId);
            cnd.desc(Sqfw_formSummary_new::getCreateDate);
            Sqfw_formSummary_new formSummary = sqfwFormsummaryNewService.fetch(cnd);
            if (formSummary != null) return;
        }

        JSONObject house = ThirdPartyService.queryDetailById(json.getString("buildingId")).getJSONObject("building");
        if (house == null) throw new ServiceException("房屋信息不存在");

        // 房屋信息
        RoomSafetyAssistantDTO dto = new RoomSafetyAssistantDTO();
        dto.setHouseId(house.getString("id"));
        dto.setCreateDate(DateUtil.format(house.getDate("createTime"), DatePattern.NORM_DATETIME_PATTERN));
        dto.setHouseName(house.getString("hname"));
        dto.setHouseGrade(house.getString("houseGrade"));
        dto.setAddress(house.getString("luQg"));

        // 隐患信息
        dto.setId(surracId); // 隐患id
        dto.setWhetherThereIsAnyHiddenDanger(json.getString("isSecurity"));
        dto.setHouseConditionDescription(json.getString("securityCondition"));
        dto.setReportName(json.getString("reportName"));
        dto.setReportTel(json.getString("reportTel"));
        dto.setVisitPictures(json.getString("photo"));

        // formSummaryNew
        Sqfw_formSummary_new formSummaryNew = new Sqfw_formSummary_new();
        formSummaryNew.setFillDate(new Date());
        formSummaryNew.setCreateDate(new Date());
        formSummaryNew.setUpdateDate(new Date());
        formSummaryNew.setType(SqfwFormSummaryConstants.HOUSE_HELPER);
        formSummaryNew.setProvince("贵州省");
        formSummaryNew.setCity("贵阳市");
        formSummaryNew.setDistrict(house.getString("districtName"));
        formSummaryNew.setCounty(house.getString("communityName"));
        formSummaryNew.setCommunity(house.getString("villageName"));
        String unitName = formSummaryNew.getProvince()+formSummaryNew.getCity();
        if (StringUtils.isNotBlank(formSummaryNew.getDistrict())) {
            unitName += formSummaryNew.getDistrict();
        }
        if (StringUtils.isNotBlank(formSummaryNew.getCounty())) {
            unitName += formSummaryNew.getCounty();
        }
        if (StringUtils.isNotBlank(formSummaryNew.getCommunity())) {
            unitName += formSummaryNew.getCommunity();
        }
        formSummaryNew.setUnitName(unitName);
        formSummaryNew.setCreateUserName(json.getString("reportName"));
        formSummaryNew.setLongitude(house.getString("lng"));
        formSummaryNew.setLatitude(house.getString("lat"));
        formSummaryNew.setParamData(JSONUtil.toJsonStr(dto));
        sqfwFormsummaryNewService.insert(formSummaryNew);

        // 第三方
        if (!isThird) {
            Object o = ThirdPartyService.check(json);
            log.info(house.getString("hname") + " 隐患id: " + o);
            if (o != null) {
                dto.setId(o.toString());
                formSummaryNew.setParamData(JSONUtil.toJsonStr(dto));
                sqfwFormsummaryNewService.updateIgnoreNull(formSummaryNew);
            }
        }
    }

    /**
     * 隐患整改
     */
    @Aop(TransAop.READ_COMMITTED)
    public void update(JSONObject json, boolean isThird) {
        log.info("隐患整改：" + json);

        String id = json.getString("surracId"); // 隐患id
        Cnd cnd = Cnd.NEW();
        cnd.and("param_data->>'id'", "=", id);
        cnd.and("param_data->>'whetherThereIsAnyHiddenDanger'", "=", "0");
        cnd.desc(Sqfw_formSummary_new::getCreateDate);
        Sqfw_formSummary_new formSummaryNew = sqfwFormsummaryNewService.fetch(cnd);

        RoomSafetyAssistantDTO dto = JSON.parseObject(formSummaryNew.getParamData(), RoomSafetyAssistantDTO.class);

        // 整改信息
        dto.setRectificationUnit(json.getString("unit"));
        dto.setAuditStatus(json.getString("auditStatus"));
        dto.setRectificationSituation(json.getString("situationAfter"));
        dto.setContactPerson(json.getString("people"));
        dto.setContactNumber(json.getString("mobile"));
        dto.setRectificationPicture(json.getString("imagesAfter"));
        dto.setConstructionPermit(json.getString("licence"));

        // formSummaryNew
        formSummaryNew.setUpdateUserName(json.getString("people"));
        formSummaryNew.setUpdateDate(new Date());
        formSummaryNew.setParamData(JSONUtil.toJsonStr(dto));
        sqfwFormsummaryNewService.updateIgnoreNull(formSummaryNew);

        // 第三方
        if (!isThird) {
            ThirdPartyService.rectification(json);
        }
    }
}
